Name :
Example2 : Bollinger

Variables : name,type,value
s,Decimal,2
p,Integer,20

Description :
This example shows how to compute the Bollinger Bands.
It is not optimized and only showed for educational purposes.

Remark : this indicator is generally displayed on the price chart. To do so, please go to the Price Properties. This window is available with a click on the wrench icon located on the left of the price chart. In this window,  click on the "Add" button to add this example indicator.


Formula :
REM Computes the Bollinger's moving average and the Standard Deviation

BollingerMA = Average[p](close)

REM Computes the Standard Deviation
REM Please note that it is much faster to use the built-in function STD

IF barindex >= p-1 THEN
	sumy2 = 0
	sumy = 0
	FOR i = 0 TO p-1 do
		sumy2 = sumy2 + SQUARE(close[i])
		sumy = sumy + close[i]
	NEXT
	STDDEV =SQRT(sumy2 / p - SQUARE(sumy / p))
ELSE
	STDDEV = undefined
ENDIF


REM We can now compute the Bollinger Bands

bollUP = BollingerMA + s * STDDEV
bollDOWN = BollingerMA - s * STDDEV

RETURN bollUP AS "Boll+", bollDOWN AS "Boll-"


